﻿using BLL.Access;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;

namespace Web.Controls.LAIHAN
{
    public partial class UCLHShenPiXX_FH : System.Web.UI.UserControl, IUCLHOperate
    {
        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        /// 存储过程BLL
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
        }
        
        public void InitControl() {
        }
        public ValidateRS Validate()
        {
            ValidateRS rs = new ValidateRS();
            rs.IsSuccess = true;
            return rs;
        }
        public void ClearInfo()
        {

        }
       
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            if (laihanxx == null || laihanxx.ID == 0)
            {
                return;
            }
            
            //先清除原计划及实用量
            this.ClearInfoContentYJH();
            this.ClearInfoContentSYL();
            //绑定原用户计划信息
            DataTable dtYJH = new YS_JH_YUANJIHUAProcess().SelectYS_JH_YUANJIHUAsDynamic("I_Y=" + (laihanxx.I_TZY == null ? laihanxx.I_Y : laihanxx.I_TZY) + " AND S_YONGHUH='" + laihanxx.S_YONGHUBH + "' AND I_JLZT=0", null);
            if (dtYJH != null && dtYJH.Rows.Count > 0)
            {
                LoadInfoContentYJH(dtYJH.Rows[0]);
            }
            int? syl, yjh;
            int? tzy = (int)(laihanxx.I_TZY == null ? laihanxx.I_Y : laihanxx.I_TZY) - 1;
            DataSet ds = new SpHelper().P_YS_LH_YONGHUYSSJCX(tzy, (int?)laihanxx.I_TZM, laihanxx.S_YONGHUBH, out yjh, out syl);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                LoadInfoContentSYL(ds.Tables[0].Rows[0]);
            }
            //绑定分户信息
            txtLX.Text = laihanxx.I_LAIHANLX.ToString();
            txtBH.Text = laihanxx.S_LAIHANBH.ToString();
            this.dtSource = _spHelper.P_YSLH_GETFENHUXX(Convert.ToInt32(laihanxx.I_LAIHANLX), laihanxx.S_LAIHANBH).Tables[0];
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            this.gvList.FocusedRowIndex = -1;
        }
        /// <summary>
        /// 清空原计划
        /// </summary>
        private void ClearInfoContentYJH()
        {
            this.yjhM1.Text = "";
            this.yjhM2.Text = "";
            this.yjhM3.Text = "";
            this.yjhM4.Text = "";
            this.yjhM5.Text = "";
            this.yjhM6.Text = "";
            this.yjhM7.Text = "";
            this.yjhM8.Text = "";
            this.yjhM9.Text = "";
            this.yjhM10.Text = "";
            this.yjhM11.Text = "";
            this.yjhM12.Text = "";
            this.yjhMSUM.Text = "";
        }

        /// <summary>
        /// 清空实用量
        /// </summary>
        private void ClearInfoContentSYL()
        {
            this.sylM1.Text = "";
            this.sylM2.Text = "";
            this.sylM3.Text = "";
            this.sylM4.Text = "";
            this.sylM5.Text = "";
            this.sylM6.Text = "";
            this.sylM7.Text = "";
            this.sylM8.Text = "";
            this.sylM9.Text = "";
            this.sylM10.Text = "";
            this.sylM11.Text = "";
            this.sylM12.Text = "";
            this.sylMSUM.Text = "";
        }
        /// <summary>
        /// 加载原计划
        /// </summary>
        private void LoadInfoContentYJH(DataRow dr)
        {
            this.yjhM1.Text = Convert.ToString(dr["I_M1"]);
            this.yjhM2.Text = Convert.ToString(dr["I_M2"]);
            this.yjhM3.Text = Convert.ToString(dr["I_M3"]);
            this.yjhM4.Text = Convert.ToString(dr["I_M4"]);
            this.yjhM5.Text = Convert.ToString(dr["I_M5"]);
            this.yjhM6.Text = Convert.ToString(dr["I_M6"]);
            this.yjhM7.Text = Convert.ToString(dr["I_M7"]);
            this.yjhM8.Text = Convert.ToString(dr["I_M8"]);
            this.yjhM9.Text = Convert.ToString(dr["I_M9"]);
            this.yjhM10.Text = Convert.ToString(dr["I_M10"]);
            this.yjhM11.Text = Convert.ToString(dr["I_M11"]);
            this.yjhM12.Text = Convert.ToString(dr["I_M12"]);
            this.yjhMSUM.Text = (int.Parse(this.yjhM1.Text)
                                + int.Parse(this.yjhM2.Text)
                                + int.Parse(this.yjhM3.Text)
                                + int.Parse(this.yjhM4.Text)
                                + int.Parse(this.yjhM5.Text)
                                + int.Parse(this.yjhM6.Text)
                                + int.Parse(this.yjhM7.Text)
                                + int.Parse(this.yjhM8.Text)
                                + int.Parse(this.yjhM9.Text)
                                + int.Parse(this.yjhM10.Text)
                                + int.Parse(this.yjhM11.Text)
                                + int.Parse(this.yjhM12.Text)
                                ).ToString();
        }

        /// <summary>
        /// 加载实用量
        /// </summary>
        private void LoadInfoContentSYL(DataRow dr)
        {
            this.sylM1.Text = Convert.ToString(dr["IM1"]);
            this.sylM2.Text = Convert.ToString(dr["IM2"]);
            this.sylM3.Text = Convert.ToString(dr["IM3"]);
            this.sylM4.Text = Convert.ToString(dr["IM4"]);
            this.sylM5.Text = Convert.ToString(dr["IM5"]);
            this.sylM6.Text = Convert.ToString(dr["IM6"]);
            this.sylM7.Text = Convert.ToString(dr["IM7"]);
            this.sylM8.Text = Convert.ToString(dr["IM8"]);
            this.sylM9.Text = Convert.ToString(dr["IM9"]);
            this.sylM10.Text = Convert.ToString(dr["IM10"]);
            this.sylM11.Text = Convert.ToString(dr["IM11"]);
            this.sylM12.Text = Convert.ToString(dr["IM12"]);
            this.sylMSUM.Text = (int.Parse(this.sylM1.Text)
                                + int.Parse(this.sylM2.Text)
                                + int.Parse(this.sylM3.Text)
                                + int.Parse(this.sylM4.Text)
                                + int.Parse(this.sylM5.Text)
                                + int.Parse(this.sylM6.Text)
                                + int.Parse(this.sylM7.Text)
                                + int.Parse(this.sylM8.Text)
                                + int.Parse(this.sylM9.Text)
                                + int.Parse(this.sylM10.Text)
                                + int.Parse(this.sylM11.Text)
                                + int.Parse(this.sylM12.Text)
                                ).ToString();
        }

        protected void btnD_Click(object sender, EventArgs e)
        {
            this.dtSource = _spHelper.P_YSLH_GETFENHUXX(Convert.ToInt32(txtLX.Text), txtBH.Text).Tables[0];
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            this.gvList.FocusedRowIndex = -1;
        }
    }
}